﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Core.Interface;
using PServiceBus.Core.Runtime.Configuration;
using PServiceBus.Core.Runtime.Extensions;

namespace PServiceBus.Cassandra {
    public sealed class KeyValueStore : IKeyValueStore {

        Provider<string> _provider;
        public KeyValueStore() : this(ConfigurationProvider.ServiceBusConfig.KeyValueStoreAddress) {
            
        }

        public KeyValueStore(string endpoint) {
            _provider = new Provider<string>()
            {
                Endpoint = endpoint,
                IDFunc = o => Guid.NewGuid()
            };
        }

        #region IKeyValueStore Members

        public ulong Incr(string key) {
            var value = _provider.Incr(key);
            if (value == 0) value = _provider.Incr(key);
            return value;
        }

        public ulong Decr(string key) {
            return _provider.Decr(key);
        }

        public void Set<TValue>(string key, TValue value) {
            _provider.Set<TValue>(key, value);
        }

        public TValue Get<TValue>(string key) {
            return _provider.Get<TValue>(key);
        }

        public void Clear() {
            _provider.ClearKeyStore();
        }

        #endregion
    }
}
